package com.lipiao.smarthome.Utils;

import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

//    "light_status":"开",
//     "air_status":"开",
//     "air_temp":"50",
//     "air_make_cold":"24",
//     "air_make_hot":"关"
//    "water_cold":"10",
//     "water_hot":"关",
//     "curtain_status":"关",
//     "sing_status":"开",
//     "tv_status":"开"
//    "tv_chan":"金鹰频道",
//     "socket_status":"通电",
//     "projector_status":"开"
public class InforUtil {

    public static InforUtil getInforUtil(String user_name) {
        if (inforUtil == null) {
            new InforUtil(user_name);
        }
        return inforUtil;

    }

    static InforUtil inforUtil;

    public static FurnitureStatusBean getFurnitureStatusBean() {
        return furnitureStatusBean;
    }

    static FurnitureStatusBean furnitureStatusBean;//json数据对象

    String furnitures[] = {"light_status", "air_status", "air_temp",
            "air_make_cold", "air_make_hot", "water_cold",
            "water_hot", "sing_status", "tv_status", "tv_chan",
            "socket_status", "projector_status"
    };
    static String queryKeys = "";
    static String userName = "";

    public InforUtil(String user_name) {
        InitQueryKeysStr();
        this.userName = user_name;
        query(user_name, queryKeys);
    }

    public void InitQueryKeysStr() {
        for (int i = 0; i <= furnitures.length - 1; i++) {
            if (i != furnitures.length - 1) {
                queryKeys = queryKeys + furnitures[i] + ",";
            } else {
                queryKeys = queryKeys + furnitures[furnitures.length - 1];
            }

        }
    }

    //查询操作（返回数据对象）
    public static void query(final String user_name, final String queryKeys) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("user_name", user_name)
                            .add("keys", queryKeys)
                            .build();
                    Request request = new Request.Builder()
                            .url("http://47.106.164.70:8889/query")
                            .post(requestBody)
                            .build();

                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    handleJson(responseData);
                    Log.d("接口调式", queryKeys + "返回的数据" + responseData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public static void handleJson(String responseData) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(responseData);
            Gson gson = new Gson();
            furnitureStatusBean = gson.fromJson(jsonObject.toString(), FurnitureStatusBean.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}


